package com.vpclub.controller;

import com.vpclub.bean.Employee;
import com.vpclub.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.util.Collection;

/**
 * @author : leimingchao
 * @date Date : 2021年08月01日 17:57
 */
@Controller
public class AjaxController {

    @Autowired
    private EmployeeDao employeeDao;


    /**
     *  @ResponseBody:
     *      将返回的数据放在响应体中
     *      如果是对象，jackson包自动将对象转为json格式
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllAjax")
    public Collection<Employee> ajaxGetAll() {
        Collection<Employee> all = employeeDao.getAll();
        return all;
    }

    /**
     * @RequestBody:
     *       请求体，获取一个请求的请求体
     *
     * @RequestParam：
     *
     *
     * @ResponseBody:可以把对象转为json数据，返回给浏览器
     *
     * @RequestBody:接收json数据，封装为对象
     *
     *
     *
     * @return
     */
    @RequestMapping(value = "/testRequestBody")
    public String testRequestBody(@RequestBody String body) {
        System.out.println("请求体：" + body);
        return "success";
    }
    @RequestMapping(value = "/test01")
    public String test01(@RequestBody String str) {
        System.out.println("请求体：" + str);
        return "success";
    }

    /**
     * 如果参数位置写HttpEntity<String> str，比@RequestBody更强，可以拿到请求头
     *
     * @RequestHeader()
     *
     *
     * @param str
     * @return
     */
    @RequestMapping(value = "/test02")
    public String test02(HttpEntity<String> str) {
        System.out.println("请求体：" + str);
        return "success";
    }

    /**
     * 将返回数据放在响应体中
     *
     *      ResponseEntity<String>:响应体中内容类型
     *
     * @return
     */
    @RequestMapping(value = "/haha")
    public ResponseEntity<String> hahah() {
        String body = "<h1>success</h1>";
        MultiValueMap<String, String> headers = new HttpHeaders();
        headers.add("Set-Cookie","username=haha");
        return new ResponseEntity<>(body, headers, HttpStatus.OK);
    }

    /**
     * SpringMVC文件下载
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request) throws Exception {
        //1.得到要下载的文件的流
        //找到要下载的文件的真实路径
        String realPath = request.getServletContext().getRealPath("/scripts/jquery-1.9.1.min.js");
        FileInputStream fileInputStream = new FileInputStream(realPath);
        //创建和文件流大小一样的字节数组
        byte[] temp = new byte[fileInputStream.available()];
        //将文件流读到字节数组中
        fileInputStream.read(temp);
        //关流
        fileInputStream.close();
        //2.将要下载的文件流返回
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Disposition","attachment;filename=" + "jquery-1.9.1.min.js");
        return new ResponseEntity<>(temp,httpHeaders,HttpStatus.OK);
    }

}
